/*
 * @Descripttion: 
 * @version: 
 * @Author: ada.wu
 * @Date: 2023-12-06 00:03:17
 * @LastEditors: ada.wu
 * @LastEditTime: 2023-12-06 00:35:02
 */

import { ref } from 'vue';
import { defineStore } from 'pinia';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';

/**
 * 
 */
export default defineStore('resource', () => {
  // 权限代码数据
  const permissionsList = ref([]);



  function getResourceList() {
    EsAjax.post(ApiUrls.resourceTreegrid, {}).then(res => {
      if (res.data.code != 200) {
        ElMessage.warning(res.data.data + res.data.msg);
        return;
      }
      // //console.log(res);
      var oneList = [];
      var twoList = [];
      for (var i = 0; i < res.data.data.length; i++) {
        var item = res.data.data[i];
        if (item.Parent == null) {
          oneList.push(item);
        } else {
          twoList.push(item);
        }
      }

      var oneIdList = [];
      for (var ii = 0; ii < oneList.length; ii++) {
        oneIdList.push(oneList[ii].Id);
      }

      var realTwoList = [];
      var realThreeList = [];
      for (var j = 0; j < twoList.length; j++) {
        var item = twoList[j];
        if (oneIdList.indexOf(item.Parent.Id) != "-1") {
          realTwoList.push(twoList[j]);
        } else {
          realThreeList.push(twoList[j]);
        }
      }

      var dataList = [];
      for (var i = 0; i < oneList.length; i++) {
        var item = oneList[i];
        if (item.LinkUrl == "/home/index") {
          var objs = {
            id: item.Id,
            path: "/",
            component: "Layout",
            redirect: "index",
            label: item.Name,
            // alwaysShow: true,
            children: [],
          };
        } else {
          var objs = {
            id: item.Id,
            path: item.LinkUrl,
            component: "Layout",
            redirect: "noRedirect",
            label: item.Name,
            // name: item.Name,
            meta: { title: item.Name, icon: "cloud" },
            alwaysShow: true,
            children: [],
          };
        }
        // //console.log(objs);
        dataList.push(objs);
      }

      var lastTwoList = [];
      for (var i = 0; i < realTwoList.length; i++) {
        var item = realTwoList[i];
        var objs = {
          parentId: item.Parent.Id,
          id: item.Id,
          path: item.LinkUrl,
          component: "EmptyLayout",
          redirect: "noRedirect",
          label: item.Name,
          mete: {
            title: item.Name,
            permissions: ["admin"],
          },
          children: [],
        };
        lastTwoList.push(objs);
      }

      for (var i = 0; i < realThreeList.length; i++) {
        var item = realThreeList[i];
        lastTwoList.forEach((items) => {
          if (items.id == item.Parent.Id) {
            return items.children.push({
              meta: { title: item.Name },
              id: item.Id,
              label: item.Name,
            });
          }
        });
      }
      for (var i = 0; i < lastTwoList.length; i++) {
        var item = lastTwoList[i];
        dataList.forEach((items) => {
          if (items.id == item.parentId) {
            return items.children.push(item);
          }
        });
      }
      console.error('dataList===', dataList)
      permissionsList.value = dataList;
    }).catch(err => {
      ElMessage.error(err.message);
    })
  }

  return {
    permissionsList,
    getResourceList
  }
}) 